﻿Imports Microsoft.Win32
Imports System.Runtime.InteropServices
Imports System.Windows.Forms

<GuidAttribute("A932BA8C-FAA4-4F7B-BC28-654EFECD87A5"), ProgId("MyFirstAddin.Addin"), ComVisible(True)>
Public Class Addin
    Implements SolidEdgeFramework.ISolidEdgeAddIn

    Public Sub OnConnection(Application As Object, ConnectMode As SolidEdgeFramework.SeConnectMode, AddInInstance As SolidEdgeFramework.AddIn) Implements SolidEdgeFramework.ISolidEdgeAddIn.OnConnection

    End Sub

    Public Sub OnConnectToEnvironment(EnvCatID As String, pEnvironmentDispatch As Object, bFirstTime As Boolean) Implements SolidEdgeFramework.ISolidEdgeAddIn.OnConnectToEnvironment
        Dim a As Guid = New Guid(EnvCatID)
        Dim b As Guid = New Guid(SECATIDS.CATID_SEDMPart)

        If a.Equals(b) Then
            ' Synchronous Part Environment
        End If
    End Sub

    Public Sub OnDisconnection(DisconnectMode As SolidEdgeFramework.SeDisconnectMode) Implements SolidEdgeFramework.ISolidEdgeAddIn.OnDisconnection

    End Sub

    <ComRegisterFunctionAttribute()> _
    Public Shared Sub RegisterFunction(ByVal t As Type)
        Dim baseKey As RegistryKey = Nothing
        Dim summaryKey As RegistryKey = Nothing
        Dim implementedCategoriesKey As RegistryKey = Nothing
        Dim environmentCategoriesKey As RegistryKey = Nothing

        Try
            baseKey = Registry.ClassesRoot.CreateSubKey(String.Format("CLSID\{0}", t.GUID.ToString("B")))
            baseKey.SetValue("AutoConnect", 1)
            baseKey.SetValue("409", "My First Addin")
            implementedCategoriesKey = baseKey.CreateSubKey(String.Format("Implemented Categories\{0}", SECATIDS.CATID_SolidEdgeAddIn))

            environmentCategoriesKey = baseKey.CreateSubKey(String.Format("Environment Categories\{0}", SECATIDS.CATID_SEAllDocumentEnvrionments))

            If Not environmentCategoriesKey Is Nothing Then
                environmentCategoriesKey.Close()
            End If

            summaryKey = baseKey.CreateSubKey("Summary")
            summaryKey.SetValue("409", "My First Addin Summary")
            summaryKey.Close()
        Catch ex As Exception
            Throw
        Finally
            If Not implementedCategoriesKey Is Nothing Then
                implementedCategoriesKey.Close()
            End If
            If Not summaryKey Is Nothing Then
                summaryKey.Close()
            End If
            If Not baseKey Is Nothing Then
                baseKey.Close()
            End If
        End Try
    End Sub

    <ComUnregisterFunctionAttribute()> _
    Public Shared Sub UnregisterFunction(ByVal t As Type)
        Try
            Registry.ClassesRoot.DeleteSubKeyTree(String.Format("CLSID\{0}", t.GUID.ToString("B")))
        Catch ex As Exception
        End Try
    End Sub
End Class
